// pages/edit/edit.js

Page({

  data:{
    //上个页面传过的图片url未上传
       url:'',  near_id:'',goods_id:'',file1:[],arr:[],last:'',time:'',name:'',flag:1,show:false,src:'',
       //编辑后的图片地址
       file:'',  fileList: [

      ],list1:[],files:[]
       
},
onShow(){
  if(this.data.flag==1){
    this.getimage11()
  }
     
},
onLoad(options){
 
  if(options.wechat){
    this.setData({wechat:options.wechat})
  }
  this.setData({
    url:options.url,
    near_id:options.near_id,
    goods_id:options.goods_id
    
  })
    
},
afterRead(event) {
  const { file } = event.detail;
  this.setData({file1:file})
  console.log(event);
  
  file.forEach((item,index)=>{
    
    wx.uploadFile({
      url: 'https://zujieba.com/Api/upload/upload.html', 
      filePath: file[index].path,
      name: 'file',
      
      success:(res)=> {
        console.log(res);
        let data1=JSON.parse(res.data)
       let url="https://zujieba.com/uploads/"+data1.name
        // 上传完成需要更新 fileList
        let fileList  = this.data.fileList;
        let list1=this.data.list1
        list1.push(data1.name)
        fileList.push({ url:url});
        console.log(fileList);
        
        this.setData({ fileList,list1 });
      },
    });
  })
  
},

navcanvas(e){
  wx.uploadFile({
    filePath:e.currentTarget.dataset.url ,
    name: 'file',
    url: 'https://zujieba.com/Api/upload/upload.html',
    success:(res)=>{
      console.log(res);
      let data=JSON.parse(res.data)
      let url="https://zujieba.com/uploads/"+data.name
     this.throttle(()=>{
      wx.navigateTo({
        url: '/pages/edit5/edit5?url='+e.currentTarget.dataset.url,
      })
     },1500)
    }
  })
  
},

 
  deleteclick2(e){
    const index=  e.detail.index
    const list=this.data.fileList
        list.splice(index,1)
       this.setData({
         fileList:list
       })
           
         
    },
  //上传
  upload(){
     wx.showLoading({
       title: '正在上传',
       icon:'none'
     })
    this.getshangchuan()
 
  },
      getshangchuan(){
      console.log(this.data.file1);
     
      wx.request({
        url: 'https://zujieba.com/Api/upload/selectionSplicingToNewPdf.html',
        data:{pdf_road_datas:this.data.list1},
        method:'POST',
        success:(res)=>{
          console.log(res);
          
          let url="https://zujieba.com/"+res.data.data
          //将图片转为pdf后需要变为临时路径预览
         wx.downloadFile({
           url: url,
           success:(res1)=>{
             console.log(res1);
           
             if(res.data.success==1){
               wx.hideLoading()
               wx.navigateTo({
                 url: '/pages/fileprint/print/newprint?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&filepath="+res1.tempFilePath+"&name="+res.data.data+
                 '&time='+res.data.addtime+'&page='+res.data.page+'&url='+url
               })
             
             }else{
               wx.showToast({
                 title: res.data.tips,
               })
             }
             
           }
         })
        
         
          
        }
      })
  },
  /*getimage1(){
    let files=this.data.files
     wx.chooseImage({
      
       success: (res) => {
         console.log(res);
         files=files.concat(res.tempFiles)
          this.setData({files})
          console.log(this.data.files);
        
         
       },
     })
  },*/
  shanchu(e){
     let files=this.data.files
     files.splice(e.currentTarget.dataset.i,1)
     this.setData({
       files,
       name:''
     })
  },
  bianji(e){
    wx.navigateTo({
      url: '/pages/edit5/edit5?url='+this.data.files[e.currentTarget.dataset.i].path+'&flag=14'+'&index3='+e.currentTarget.dataset.i,
    })
  },
 /* upload1(){
    console.log(11);
    
    wx.showLoading({
      title: '正在上传中',
      icon:'none'
    })
    let arr=[]
    this.data.files.forEach((item,index)=>{
      wx.uploadFile({
        filePath: item.path,
        name: 'file',
        url: 'https://zujieba.com/Api/upload/upload.html',
        success:(res)=>{
              console.log(res);
             
              
              let data=JSON.parse(res.data)
              arr.push(data.name)
               this.setData({arr})
               
               
               if(arr.length==this.data.files.length){
                 console.log(111);
                 console.log(arr);
                 this.getpdfimage(arr)
                  
               }
              
        }
      })
    })
  },*/
  upload1(){
   this.throttle(()=>{
    let url="https://zujieba.com/uploads/"+this.data.name
    let time=this.data.time
    let name=this.data.name
    if(name==''){return}
    // wx.navigateTo({
    //   url: '/pages/fileprint/print/print?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&name="+name+
    //   '&time='+time+'&page=1'+"&url="+url+'&radio5=3'+"&photo=1"+'&param1=1'
    // })
    wx.navigateTo({
      url: '/pages/fileprint/print/newprint?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&name="+name+
      '&time='+time+'&page=1'+"&url="+url+'&radio5=4'+"&photo=1"+'&param1=1'
    })
   },1000)
  },
  getpdfimage(file){
    wx.request({
      url: 'https://zujieba.com/Api/upload/selectionSplicingToNewPdf.html',
      data:{pdf_road_datas:file},
      method:'POST',
      success:(res)=>{
        console.log(res);
        
        let url="https://zujieba.com/"+res.data.data
        //将图片转为pdf后需要变为临时路径预览
       wx.downloadFile({
         url: url,
         success:(res1)=>{
           console.log(res1);
         
           if(res.data.success==1){
             wx.hideLoading()
            //  wx.navigateTo({
            //    url: '/pages/fileprint/print/print?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&filepath="+url+"&name="+res.data.data+
            //    '&time='+res.data.addtime+'&page='+res.data.page+"&url="+url+'&radio5=4'
            //  })
            wx.navigateTo({
              url: '/pages/fileprint/print/newprint?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&filepath="+url+"&name="+res.data.data+
              '&time='+res.data.addtime+'&page='+res.data.page+"&url="+url+'&radio5=4'
            })
           
           }else{
             wx.showToast({
               title: res.data.tips,
               icon:'none'
             })
           }
           
         }
       })
      
       
        
      }
    })

  },
  getceshiphoto(){
    var that=this
    // getimage1
    wx.showActionSheet({
      itemList: ['拍照','从相册中选取'],
      itemColor: '#',
      success(res) {
          console.log(res.tapIndex);
          if (res.tapIndex === 0) {
              //调用camera组件
              that.setData({show:true})
          } else if (res.tapIndex === 1) {
              wx.chooseImage({
                  count: 1,
                  sizeType: ["compressed"],
                  sourceType: ['album'],
                  success: function (res1) {
                  },
                  fail: function () {
                  }
              })
          }
      }
  })
  },
  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          files: [{path:res.tempImagePath}],
          show:false
        })
      }
    })
  },
  getimage11(){
    wx.uploadFile({
      filePath: this.data.name,
      name: 'file',
      url: 'https://zujieba.com/Api/upload/upload.html',
      success:(res1)=>{
        console.log(res1);
        let data=JSON.parse(res1.data)
        console.log(data);
        this.setData({
          time:data.time,

          name:data.name})} })
  },
  getimage1(){
    if(this.data.wechat==1){
      wx.chooseMessageFile({
        count:1,
        type:'image',
        success:(res)=>{
            console.log(res);
            let files=this.data.files
            files.push(res.tempFiles[0])
            this.setData({files})
             wx.uploadFile({
               filePath: res.tempFiles[0].path,
               name: 'file',
               url: 'https://zujieba.com/Api/upload/upload.html',
               success:(res1)=>{
                 console.log(res1);
                 let data=JSON.parse(res1.data)
                 console.log(data);
                 this.setData({
                   time:data.time,
 
                   name:data.name,
                   
                 })
                 
               }
             })
            
        }
      })

      return
    }

     wx.chooseImage({
       count:1,
       success:(res)=>{
           console.log(res);
           let files=this.data.files
           files.push(res.tempFiles[0])
           this.setData({files})
            wx.uploadFile({
              filePath: res.tempFiles[0].path,
              name: 'file',
              url: 'https://zujieba.com/Api/upload/upload.html',
              success:(res1)=>{
                console.log(res1);
                let data=JSON.parse(res1.data)
                console.log(data);
                this.setData({
                  time:data.time,

                  name:data.name,
                  
                })
                
              }
            })
           
       }
     })
  },
   //节流
   throttle:function(func,wait){
    console.log(func,wait);
    
    let last =this.data.last
    console.log(last);
    
    let current_time= +new Date()
    if(current_time-last>wait){
      console.log(1111);
        console.log(current_time,current_time-last);
        
        func()
        last=+new Date()
        this.setData({
          last
        })
    }
      
       
    
     
  },
  
  
  
})